import os, time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import urllib.request

#远程调试
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

driver = webdriver.Chrome(options=chrome_options)

driver.get('https://www.baidu.com')

driver.find_element_by_xpath('//*[@id="kw"]').send_keys('深圳')
driver.find_element_by_xpath('//*[@id="su"]').click()
time.sleep(2)
driver.find_element_by_link_text('图片').click()

# 定位图片元素
Images = driver.find_elements_by_css_selector('div.imgbox img[class="main_img img-hover"]')

i = 0
for x in Images:
    url = x.get_attribute('data-imgurl')
    urllib.request.urlretrieve(url, str(i)+'img.jpg')
    i += 1
    if i == 5:
        break

# 关闭浏览器
driver.quit()






